package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.repository.UserRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AsserUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void insert(UserInfo userInfo) {
        userRepository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userRepository.deleteById(id);
    }

    @Override
    public void update(UserInfo userInfo) {
        userRepository.save(userInfo);
    }

    @Override
    public UserInfo selectOne(String id) {
        Optional<UserInfo> userInfo = userRepository.findById(id);
        return userInfo.orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return userRepository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = userRepository.findByPhone(phone);
        return userInfo != null;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //创建验证码
        String substring = UUID.randomUUID().toString().replaceAll("-", "").substring(0,4);

        //创建短信
        StringBuilder stringBuilder = new StringBuilder(80);
        stringBuilder.append("【创信】您注册的短信验证码是:").append(substring).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");

      /*  //Springmvc提供一个工具类:RestTemplate用于发起http请求
        RestTemplate template = new RestTemplate();

        //参数1:短信接口url
        String url = "https://way.jd.com/chuangxin/dxjk?mobile={1}&content={2}&appkey={3}";
        //参数2:请求接口往之后相应数据封装对象类型
        //参数3:请求参数列表
        String ret = template.getForObject(url, String.class,phone,stringBuilder,"3d17670c108335e96d97ec01214590bc" );


        //判断是否发送成功
        if (!ret.contains("Success")){
            throw new LogicException("发送短信失败");
        }
*/

        //调用redis方法传到内存
        userInfoRedisService.sendVerifyCode(phone,substring);

        System.out.println(stringBuilder);
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //1.传到后端后,来到service层,要先校验参数是否为空
        AsserUtil.isLength(phone, "账号不能为空");
        AsserUtil.isLength(nickname, "昵称不能为空");
        AsserUtil.isLength(password, "密码不能为空");
        AsserUtil.isLength(rpassword, "确认密码不能为空");
        AsserUtil.isLength(verifyCode, "验证码不能为空");

        //2.校验2次输入密码是否相等
        AsserUtil.isEquals(password, rpassword, "两次密码验证不正确");

        //3.校验手机号码是否唯一
        if (this.checkPhone(phone)){
            throw new LogicException("手机号已存在");
        }

        //4.校验短信验证码是否正确
        String equalsVerifyCode = userInfoRedisService.getVerifyCode(phone);
        if (equalsVerifyCode == null){
            throw new LogicException("请先发送验证码");
        }
        //验证码无视大小写
        if (!equalsVerifyCode.equalsIgnoreCase(verifyCode)){
             throw new LogicException("验证码错误");
        }

        //5.注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setCity("");
        userInfo.setEmail("");
        userInfo.setHeadImgUrl("/images/default.png");
        userInfo.setState(UserInfo.STATE_NORMAL);
        userRepository.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        UserInfo userInfo = userRepository.findByPhone(username);
        if (userInfo == null || !userInfo.getPassword().equals(password)){
            throw new LogicException("账号或密码错误");
        }
        userInfo.setPassword("");
        return userInfo;
    }

    @Override
    public List<UserInfo> findByDestName(String keyword) {
        List<UserInfo> userInfos = userRepository.findByCity(keyword);
        return userInfos;
    }
}
